﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using c_work.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Work.Models;

namespace Work.Controllers
{
    [Route("[Controller]/[Action]")]
    [ApiController]
    public class BlogTypesController : Controller
    {
        private readonly DBContext _context;

        public BlogTypesController(DBContext context)
        {
            _context = context;
        }

        [HttpGet]
        [NoSign]
        // GET: BlogTypes
        public async Task<string> Index()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            var list = await _context.BlogTypes.ToListAsync();
            dic.Add("data", list);
            dic.Add("code", 0);
            // dic.Add("msg", "成功");
            return Newtonsoft.Json.JsonConvert.SerializeObject(dic);
        }

        // GET: BlogTypes/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var blogType = await _context.BlogTypes
                .FirstOrDefaultAsync(m => m.Id == id);
            if (blogType == null)
            {
                return NotFound();
            }

            return View(blogType);
        }

        // GET: BlogTypes/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: BlogTypes/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Typename")] BlogType blogType)
        {
            if (ModelState.IsValid)
            {
                _context.Add(blogType);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(blogType);
        }

        // GET: BlogTypes/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var blogType = await _context.BlogTypes.FindAsync(id);
            if (blogType == null)
            {
                return NotFound();
            }
            return View(blogType);
        }

        // POST: BlogTypes/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Typename")] BlogType blogType)
        {
            if (id != blogType.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(blogType);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BlogTypeExists(blogType.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(blogType);
        }

        // GET: BlogTypes/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var blogType = await _context.BlogTypes
                .FirstOrDefaultAsync(m => m.Id == id);
            if (blogType == null)
            {
                return NotFound();
            }

            return View(blogType);
        }

        // POST: BlogTypes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var blogType = await _context.BlogTypes.FindAsync(id);
            _context.BlogTypes.Remove(blogType);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool BlogTypeExists(int id)
        {
            return _context.BlogTypes.Any(e => e.Id == id);
        }
    }
}
